﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Big_Data.Excel;
using Big_Data.Data_Base_Excel;
using System.Configuration;
using Big_Data.Entities;
using Big_Data.Data_Base_Excel.Offres_Facteurs_Clés;

namespace Big_Data
{
    public partial class Form1 : Form
    {
        Excel_File excel = new Excel_File();
        public string fileName = string.Empty;
        public BackgroundWorker worker { get; set; }

        public Form1()
        {
            InitializeComponent();
        }
        
        public string OpenFile()
        {
            try
            {
                openFileDialog1.FileName = string.Empty;
                openFileDialog1.Filter = "Excel file (*.xls,*.xlsx)|*.xls;*.xlsx";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.textBoxPathExcelFile.Text = openFileDialog1.FileName;
                    fileName = openFileDialog1.SafeFileName.Trim();
                    fileName = fileName.Substring(0,fileName.IndexOf('.'));
                    labelNameFile.Text = fileName;
                    return this.textBoxPathExcelFile.Text;
                }
                return string.Empty;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }

        public void AddCheckBoxSheet()
        {
            checkedListBox1.Items.Clear();
            List<string> checkboxlst = excel.ExcelScanNameSheet().Where(w=>!w.Contains("Graph")).ToList();
            foreach (var item in checkboxlst)
            {
                checkedListBox1.Text = item;
                checkedListBox1.Items.Add(item);
            }
            //adjustSize(checkedListBox1);
            checkedListBox1.Visible = true;
            for (int i = 1; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemCheckState(i, CheckState.Checked);
            }
            #region CheckBox
            //List<string> itemName = excel.ExcelScanNameSheet();
            //Panel panel = new Panel();

            //int locationX = 0;
            //int locationY = 0;
            //int locationPannelX = panel.Location.X;
            //int locationPannelY = panel.Location.Y;
            //int cmpt = 0;
            //int nbItemName = itemName.Count;
            //double nbVerticalCheckBox = Math.Ceiling((double)nbItemName / 3);

            //for (int i = 0; i < itemName.Count; i++)
            //{
            //    checkBox = new CheckBox();

            //    checkBox.Text = itemName[i];
            //    checkBox.AutoSize = true;
            //    checkBox.Location = new Point(locationPannelX, locationPannelY);
            //    locationPannelY = locationPannelY + checkBox.Size.Height + 5;
            //    panel.Controls.Add(checkBox);

            //    cmpt++;

            //    if (cmpt == nbVerticalCheckBox || i == nbItemName - 1)
            //    {
            //        cmpt = 0;
            //        panel.Location = new Point(locationX, locationY);
            //        panel.AutoSize = true;
            //        panel1.Controls.Add(panel);
            //        panel = new Panel();
            //        locationPannelY = panel.Location.Y;
            //        locationX = locationX + panel.Size.Width + 5;
            //    }
            //}
            #endregion
        }

        public void adjustSize(ListBox lst)
        {
            int w = lst.Width * lst.Items.Count;
            lst.Width = w+ lst.Width - lst.ClientSize.Width;
        }

        private void buttonOpen_Click(object sender, EventArgs e)
        {
            this.textBoxPathExcelFile.Text = OpenFile();
        }

        private void buttonExcelOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.textBoxPathExcelFile.Text))
            {
                MessageBox.Show("Selectionner le fichier excel");
                return;
            }
            try
            {
                //excel.Application();
                excel.path = this.textBoxPathExcelFile.Text;
                excel.ExcelOpen();
                AddCheckBoxSheet();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void Add(BackgroundWorker worker,DoWorkEventArgs e)
        {
            //Import import = new Import();
            //foreach (var itemChecked in checkedListBox1.CheckedItems)
            //{
            //    int index = checkedListBox1.Items.IndexOf(itemChecked);
            //    excel.OpenSheet(itemChecked.ToString());
            //    object[,] valueArray = excel.ExcelScanValueSheet();

            //    if (index == 0)
            //    {
            //        import.ImportSheet1ToDataBase(valueArray, worker, e);
            //    }

            //}
            #region
            //Form2 form2 = new Form2();
            //form2.pathExcel = textBoxPathExcelFile.Text;
            //form2.Show();
            //this.Hide();

            //excel.CleanUp();
            #endregion
        }

        private void buttonSuivant_Click(object sender, EventArgs e)
        {
            string file = ConfigurationManager.AppSettings["offres_facteurs"];

            if(checkedListBox1.SelectedItems.Count == 0)
            {
                MessageBox.Show("Selectionner un sheet ou plusieurs");
                return;
            }
            try
            {
                #region
                //foreach (var itemChecked in checkedListBox1.CheckedItems)
                //{
                //    int index = checkedListBox1.Items.IndexOf(itemChecked);
                //    excel.OpenSheet(itemChecked.ToString());
                //    object[,] valueArray = excel.ExcelScanValueSheet();

                //    if (index == 0)
                //    {
                //        import.ImportSheet1ToDataBase(valueArray);
                //    }

                //}
                //Form2 form2 = new Form2();
                //form2.pathExcel = textBoxPathExcelFile.Text;
                //form2.Show();
                //this.Hide();
                #endregion
                if (!backgroundWorker1.IsBusy)
                {
                    backgroundWorker1.RunWorkerAsync();
                    this.buttonSuivant.Enabled = false;
                    this.buttonCancel.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                if (e.Cancelled)
                    this.labelError.Text = "Annuler";
                else if (!(e.Error == null))
                {
                    this.labelError.Text = "Erreur: " + e.Error.Message;
                    MessageBox.Show(e.Error.Message);
                }
                else
                {
                    this.labelProgress.Text = "Done!";
                    Form2 form2 = new Form2();
                    form2.pathExcel = textBoxPathExcelFile.Text;
                    form2.Show();
                    this.Hide();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                excel.CleanUp();
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            worker = sender as BackgroundWorker;
            Add(worker,e);
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.progressBar1.Value = e.ProgressPercentage;
            this.labelProgress.Text = e.ProgressPercentage.ToString() + "%";
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.WorkerSupportsCancellation)
            {
                backgroundWorker1.CancelAsync();
                this.buttonCancel.Enabled = false;
                this.buttonSuivant.Enabled = true;
            }
        }

        private void radioButtonNonImport_CheckedChanged(object sender, EventArgs e)
        {
             if(this.radioButtonOuiImport.Checked)
            {
                Form3 form3 = new Form3();
                form3.ShowDialog();
            }
        }
    }
}
